//
//  MaterViewController.m
//  LastTemp
//
//  Created by dllo on 15/4/24.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MaterViewController.h"
#import "NetWorkingTool.h"
#import "MaterCell.h"
#import "MaterModel.h"
#import "HomePage.h"
#import "MaterHeadView.h"
#import "AVSingleton.h"
#import "drawerView.h"

#import "UIImageView+WebCache.h"
#import "BassModel.h"
#import "bofangViewController.h"
#import "AFSoundManager.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface MaterViewController ()
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)NSMutableArray *scrollArr;
@property(nonatomic,retain)NSMutableArray *giveArr;
@property(nonatomic,retain)NSMutableArray *songArr;
@property(nonatomic,retain)drawerView *drawer;
@property(nonatomic,retain)UIImageView *smallImageView;
@property(nonatomic,retain)UILabel *smallLabel;
@property(nonatomic,assign)NSInteger num;
@property(nonatomic,retain)NSMutableArray *playMaterArr;


@property(nonatomic,retain)UIActivityIndicatorView *activity;

@end

@implementation MaterViewController

- (void)dealloc
{
    [_activity release];
    [_pageId1 release];
    [_pageId2 release];
    [_pageId3 release];
    [_tableView release];
    [_scrollArr release];
    [_giveArr release];
    [_songArr release];
    [_drawer release];
    [_smallImageView release];
    [_smallLabel release];
    [_playMaterArr release];
    [super dealloc];
}
- (void)dataNewGive
{
 
//    if ([AVSingleton shareSingleton].internetNum == 1) {
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您现在没有网络,请检查网络!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
//        [alert show];
//        [alert release];
//    } else {
    NSString *str = [NSString stringWithFormat:@"http://iting.music.189.cn:9200/iting2/imusic/V2?resid=%@&page=1&size=10&protocolCode=2.0&method=get_user_song_single_details&sid=234472872&networkType=wifi&serviceCode=100&sessionId=83356C7DD4CDA5C8EE072C41746AA3A6&format=json&timestamp=20150423220423&sign=7a905adab1b39a007133df59f8bd232f&imsi=87D618C0-A675-458D-BB55-BED13C712771&company=&appChannelCode=10000000&appVerCode=2.000.004",self.pageId2];
    [NetWorkingTool netWorkingToolStr:str block:^(id result) {
        NSMutableDictionary *dic = result;
        NSMutableDictionary *dic1 = dic[@"data"];
        NSMutableArray *songlistArr = dic1[@"songlist"];
        for (NSInteger i = 0; i < songlistArr.count; i++) {
             MaterModel *mater = [[MaterModel alloc] init];
            [mater setValuesForKeysWithDictionary:dic1];
            [mater setValuesForKeysWithDictionary:songlistArr[i]];
            NSMutableArray *qq_url_listArr = songlistArr[i][@"qqInfo"][@"qq_url_list"];
            NSMutableDictionary *dic = [qq_url_listArr objectAtIndex:0];
            [mater setValuesForKeysWithDictionary:dic];
            [self.giveArr addObject:mater];
            [mater release];
        }
        
        [self.tableView reloadData];
        
    }];
//    }
}
- (void)dataSong
{
//    if ([AVSingleton shareSingleton].internetNum == 1) {
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您现在没有网络,请检查网络!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
//        [alert show];
//        [alert release];
//    } else {
    NSString *str = [NSString stringWithFormat:@"http://iting.music.189.cn:9200/iting2/imusic/V2?resid=%@&page=1&size=10&protocolCode=2.0&method=get_album_songs&sid=234472872&networkType=wifi&serviceCode=100&sessionId=83356C7DD4CDA5C8EE072C41746AA3A6&format=json&timestamp=20150423220750&sign=d918e71f69e3eacf96cc74999617d519&imsi=87D618C0-A675-458D-BB55-BED13C712771&company=&appChannelCode=10000000&appVerCode=2.000.004",self.pageId3];
    [NetWorkingTool netWorkingToolStr:str block:^(id result) {
        NSMutableDictionary *dic = result;
        NSMutableArray *songlistArr = dic[@"songlist"];
        for (NSInteger i = 0; i < songlistArr.count; i++) {
            MaterModel *mater = [[MaterModel alloc] init];
            [mater setValuesForKeysWithDictionary:dic];
            [mater setValuesForKeysWithDictionary:songlistArr[i]];
            NSMutableArray *qq_url_listArr = songlistArr[i][@"qqInfo"][@"qq_url_list"];
            NSMutableDictionary *dic = [qq_url_listArr objectAtIndex:0];
            [mater setValuesForKeysWithDictionary:dic];
            [self.songArr addObject:mater];
            [mater release];
            
        }
        
        
        [self.tableView reloadData];
        
    }];
//    }
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.scrollArr = [NSMutableArray array];
        self.giveArr = [NSMutableArray array];
        self.songArr = [NSMutableArray array];
           }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if ([AVSingleton shareSingleton].internetNum == 1) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您现在没有网络,请检查网络!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
        [alert release];
    } else {
    [self dataMater];
    [self dataSong];
    [self dataNewGive];
    }
    
    
    [self addTableView];
    
    self.activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(WIDTH/2, 100, 30, 30)];
    [self.activity setCenter:CGPointMake(self.view.frame.size.width / 2, (self.view.frame.size.height - 113) / 2)]; // 指定进度轮中心点
    [self.activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    [self.view addSubview:self.activity];
    [self.activity startAnimating];
    [self.activity release];

   
}


- (void)addTableView
{
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-49) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.rowHeight = 120;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    MaterHeadView *headView = [[MaterHeadView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 150)];
    self.tableView.tableHeaderView = headView;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UIImageView *headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 150)];
    [headView addSubview:headImageView];
    [headImageView release];
    headImageView.image = [UIImage imageNamed:@"once1"];
    self.smallImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH/2-50, 10, 100, 100)];
    self.smallImageView.layer.cornerRadius = 30;
    [headView addSubview:self.smallImageView];
    [_smallImageView release];
    self.smallImageView.clipsToBounds = YES; // 出框
    self.smallImageView.contentMode = UIViewContentModeScaleAspectFill; //设置出框效果
    
    self.smallLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 115, WIDTH-30, 20)];
    [headView addSubview:self.smallLabel];
    [_smallLabel release];
    self.smallLabel.font = [UIFont systemFontOfSize:15];
    self.smallLabel.textAlignment = NSTextAlignmentCenter;
   
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (self.scrollArr.count > 0 || self.giveArr.count > 0 || self.songArr.count >0) {
        [self.activity removeFromSuperview];
    }
    
    
    
    
    
    if (self.temp == 1) {
        return self.scrollArr.count;
    } else if (self.temp == 2) {
        return self.giveArr.count;
    } else {
        return self.songArr.count;
    }
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    MaterCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[[MaterCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }
    if (self.temp == 1) {
        MaterModel *mater = self.scrollArr[indexPath.row];
        cell.topLabel.text = mater.song_name;
        cell.nameLabel.text = mater.singer_name;
        NSURL *url = [NSURL URLWithString:mater.pic_url];
        [self.smallImageView sd_setImageWithURL:url];
        self.smallLabel.text = mater.songlistdes;
        self.title = mater.songlistname;

    } else if (self.temp == 2){
        MaterModel *mater = self.giveArr[indexPath.row];
        cell.topLabel.text = mater.song_name;
        cell.nameLabel.text = mater.singer_name;
        NSURL *url = [NSURL URLWithString:mater.pic_url];
        [self.smallImageView sd_setImageWithURL:url];
        self.smallLabel.text = mater.songlistdes;
        self.title = mater.songlistname;

    } else if(self.temp == 3){
        MaterModel *mater = self.songArr[indexPath.row];
        cell.topLabel.text = mater.song_name;
        cell.nameLabel.text = mater.singer_name;
        NSURL *url = [NSURL URLWithString:mater.pic_url];
        [self.smallImageView sd_setImageWithURL:url];
        self.smallLabel.text = mater.singer_name;
        self.title = mater.title;

    }
        cell.myImageView.image = [UIImage imageNamed:@"02"];
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     self.playMaterArr = [NSMutableArray array];
    bofangViewController *play = [[bofangViewController alloc] init];
    if (self.temp == 1) {
        MaterModel *mater = self.scrollArr[indexPath.row];
        if ([AVSingleton shareSingleton].fromNum != indexPath.row || [AVSingleton shareSingleton].fromNum != 0) {
            [[AFSoundManager sharedManager] stop];
        }
        [[AFSoundManager sharedManager] startStreamingRemoteAudioFromURL:mater.url andBlock:^(int percentage, CGFloat elapsedTime, CGFloat timeRemaining, NSError *error, BOOL finished) {
            [AVSingleton shareSingleton].percentage = percentage;
            NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
            [formatter setDateFormat:@"mm:ss"];
            NSDate *elapsedTimeDate = [NSDate dateWithTimeIntervalSince1970:elapsedTime];
            [AVSingleton shareSingleton].beginTime = [formatter stringFromDate:elapsedTimeDate];
            
            NSDate *timeRemainingDate = [NSDate dateWithTimeIntervalSince1970:timeRemaining];
            [AVSingleton shareSingleton].time = [formatter stringFromDate:timeRemainingDate];
            
            
        }];
        for (NSInteger i = 0; i < self.scrollArr.count; i++) {
            MaterModel *mater1 = self.scrollArr[i];
            BassModel *base = [[BassModel alloc] init];
            base.name1 = mater1.song_name;
            base.singerName1 = mater1.singer_name;
            base.url1 = mater1.url;
            base.picURL = mater1.singer_pic_url;
            [self.playMaterArr addObject:base];
        }
        [AVSingleton shareSingleton].fromArr = self.playMaterArr;
        [AVSingleton shareSingleton].fromNum = indexPath.row;
        [self presentViewController:play animated:YES completion:^{
            
        }];
        
  
        [AVSingleton shareSingleton].myBool = YES;
        [play release];
        
    } else if (self.temp == 2) {
    MaterModel *mater = self.giveArr[indexPath.row];
        if ([AVSingleton shareSingleton].fromNum != indexPath.row || [AVSingleton shareSingleton].fromNum != 0) {
            [[AFSoundManager sharedManager] stop];
        }
        [[AFSoundManager sharedManager] startStreamingRemoteAudioFromURL:mater.url andBlock:^(int percentage, CGFloat elapsedTime, CGFloat timeRemaining, NSError *error, BOOL finished) {
            [AVSingleton shareSingleton].percentage = percentage;
            NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
            [formatter setDateFormat:@"mm:ss"];
            NSDate *elapsedTimeDate = [NSDate dateWithTimeIntervalSince1970:elapsedTime];
            [AVSingleton shareSingleton].beginTime = [formatter stringFromDate:elapsedTimeDate];
            
            NSDate *timeRemainingDate = [NSDate dateWithTimeIntervalSince1970:timeRemaining];
            [AVSingleton shareSingleton].time = [formatter stringFromDate:timeRemainingDate];
            
            
        }];

        for (NSInteger i = 0; i < self.giveArr.count; i++) {
            MaterModel *mater1 = self.giveArr[i];
            BassModel *base = [[BassModel alloc] init];
            base.name1 = mater1.song_name;
            base.singerName1 = mater1.singer_name;
            base.url1 = mater1.url;
            base.picURL = mater1.singer_pic_url;
            [self.playMaterArr addObject:base];
        }
        [AVSingleton shareSingleton].fromArr = self.playMaterArr;
         [AVSingleton shareSingleton].fromNum = indexPath.row;
        [self presentViewController:play animated:YES completion:^{
            
        }];
      
         [AVSingleton shareSingleton].myBool = YES;
        [play release];
        
    } else if (self.temp == 3) {
        MaterModel *mater = self.songArr[indexPath.row];
        if ([AVSingleton shareSingleton].fromNum != indexPath.row || [AVSingleton shareSingleton].fromNum != 0) {
            [[AFSoundManager sharedManager] stop];
        }
        [[AFSoundManager sharedManager] startStreamingRemoteAudioFromURL:mater.url andBlock:^(int percentage, CGFloat elapsedTime, CGFloat timeRemaining, NSError *error, BOOL finished) {
            [AVSingleton shareSingleton].percentage = percentage;
            NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
            [formatter setDateFormat:@"mm:ss"];
            NSDate *elapsedTimeDate = [NSDate dateWithTimeIntervalSince1970:elapsedTime];
            [AVSingleton shareSingleton].beginTime = [formatter stringFromDate:elapsedTimeDate];
            
            NSDate *timeRemainingDate = [NSDate dateWithTimeIntervalSince1970:timeRemaining];
            [AVSingleton shareSingleton].time = [formatter stringFromDate:timeRemainingDate];
            
            
        }];

        for (NSInteger i = 0; i < self.songArr.count; i++) {
            MaterModel *mater1 = self.songArr[i];
            BassModel *base = [[BassModel alloc] init];
            base.name1 = mater1.song_name;
            base.singerName1 = mater1.singer_name;
            base.url1 = mater1.url;
            base.picURL = mater1.singer_pic_url;
            [self.playMaterArr addObject:base];
        }
        [AVSingleton shareSingleton].fromArr = self.playMaterArr;
         [AVSingleton shareSingleton].fromNum = indexPath.row;
         [AVSingleton shareSingleton].myBool = YES;
        [self presentViewController:play animated:YES completion:^{
            
        }];
        [play release];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)dataMater
{
    
//    if ([AVSingleton shareSingleton].internetNum == 1) {
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您现在没有网络,请检查网络!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
//        [alert show];
//        [alert release];
//    } else {
    NSString *url = [NSString stringWithFormat:@"http://iting.music.189.cn:9200/iting2/imusic/V2?resid=%@&page=1&size=10&protocolCode=2.0&method=get_user_song_single_details&sid=234472872&networkType=wifi&serviceCode=100&sessionId=83356C7DD4CDA5C8EE072C41746AA3A6&format=json&timestamp=20150423220423&sign=7a905adab1b39a007133df59f8bd232f&imsi=87D618C0-A675-458D-BB55-BED13C712771&company=&appChannelCode=10000000&appVerCode=2.000.004",self.pageId1];
    [NetWorkingTool netWorkingToolStr:url block:^(id result) {
        NSMutableDictionary *dic = result;
        NSMutableDictionary *dic1 = dic[@"data"];
        NSMutableArray *songlistArr = dic1[@"songlist"];
        for (NSInteger i = 0; i < songlistArr.count; i++) {
            MaterModel *mater = [[MaterModel alloc] init];
            [mater setValuesForKeysWithDictionary:dic1];
            [mater setValuesForKeysWithDictionary:songlistArr[i]];
            NSMutableArray *qq_url_listArr = songlistArr[i][@"qqInfo"][@"qq_url_list"];
            NSMutableDictionary *dic = [qq_url_listArr objectAtIndex:0];
            [mater setValuesForKeysWithDictionary:dic];
            [self.scrollArr addObject:mater];
            [mater release];
        }
        
        
        [self.tableView reloadData];
        
    }];
//    }
}





@end
